package crmdna.api.servlet;

import crmdna.common.APIResponse;
import crmdna.common.RequestInfo;
import crmdna.common.Utils;
import crmdna.common.APIResponse.Status;
import crmdna.member.MemberLoader;
import crmdna.member.MemberProp;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MemberQuickSearchServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		HttpSession session = request.getSession(true);		
		String client = request.getParameter("client");
		if(client == null)
			client = "isha";
		
		String searchStr = request.getParameter("searchStr");
		Set<Long> groupIds = null;
		Integer maxResultSize = null;
		String login = session.getAttribute("login").toString();
		
		try {
			List<MemberProp> memberProps = MemberLoader.quickSearch(client,
					searchStr, groupIds, maxResultSize, login);

			new APIResponse()
					.status(Status.SUCCESS)
					.object(memberProps)
					.message("No of records: " + memberProps.size())
					.setJsonResponse(response);
			
		} catch (Exception ex) {
			Utils.toAPIResponse(ex, true, new RequestInfo()
					.client(client).req(request).login(login)).
					setJsonResponse(response);
		}
	}
}